package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/
 * @create 2025/7/27 18:51
 * @since JDK17
 */

public class Solution01 {
    public int maxFreeTime(int et, int k, int[] startTime, int[] endTime) {
        int s = 0, ans = 0, n = startTime.length, sum = 0, e;
        for (int l = 0, r = 0; l < n; l++) {
            while (r < n && r - l < k) {    // 窗口还能扩充
                sum += endTime[r] - startTime[r];   // 窗口的大小
                r++;
            }
            e = r == n ? et : startTime[r]; // 否则等于下一个活动的开始时间
            ans = Math.max(ans, e - s - sum);
            sum -= endTime[l] - startTime[l];
            s = endTime[l]; // 等于上一个活动的结束时间
        }

        return ans;
    }
}
